<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleDetail">添加新房楼盘</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">楼盘标题：</span>
        <el-input placeholder="请输入楼盘标题" v-model="searchParams.title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">上架状态：</span>
        <el-select v-model="searchParams.shangjia_status" placeholder="请选择上架状态" clearable>
          <el-option label="上架" :value="1"/>
          <el-option label="下架" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">创建时间：</span>
        <el-date-picker
            v-model="searchParamsTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
        />
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)" flexible>
      <el-table-column prop="new_house_sn" label="楼盘">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="编号">{{ row.new_house_sn }}</el-descriptions-item>
            <el-descriptions-item label="标题">{{ row.title }}  <el-tag type="success" style="margin-left: 10px" v-if="row.is_tuijian == 1">推荐</el-tag> </el-descriptions-item>
            <el-descriptions-item label="开发商">{{ row.developer_name }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="new_house_sn" label="基础信息"  width="200">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="面积">{{ row.min_area_num }}~{{ row.max_area_num }}</el-descriptions-item>
            <el-descriptions-item label="绿化率">{{ row.lvhua_ratio }}</el-descriptions-item>
            <el-descriptions-item label="容积率">{{ row.rongji_ratio }}</el-descriptions-item>
            <el-descriptions-item label="总户数">{{ row.total_house_num }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>

      <el-table-column prop="pianqu_title" label="位置">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="所属片区">{{ row.pianqu_title }}</el-descriptions-item>
            <el-descriptions-item label="经度,纬度">{{ row.lng }},{{ row.lat }}</el-descriptions-item>
            <el-descriptions-item label="省市区">{{ row.province }}/{{ row.city }}/{{ row.district }}</el-descriptions-item>
            <el-descriptions-item label="详细地址">{{ row.addr }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="pianqu_title" label="价格" width="200">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="单价">{{ row.one_price }}万/平</el-descriptions-item>
            <el-descriptions-item label="最低总价">{{ row.min_total_price }}</el-descriptions-item>
            <el-descriptions-item label="最高总价">{{ row.max_total_price }}</el-descriptions-item>
            <el-descriptions-item label="物业费">{{ row.wuye_price }}元/平米/月</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核"  width="200">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="状态">
              <el-text v-if="row.status==1" type="warning">待审核</el-text>
              <el-text v-if="row.status==2" type="success">已通过</el-text>
              <el-text v-if="row.status==3" type="danger">未通过</el-text>
            </el-descriptions-item>
            <el-descriptions-item label="审核意见">{{ row.check_cause }}</el-descriptions-item>
            <el-descriptions-item label="审核时间">{{ row.check_time }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="sell_status" label="出售状态" width="80">
        <template #default="{ row }">
          <el-text v-if="row.sell_status==1" type="success">{{row.sell_status_label}}</el-text>
          <el-text v-if="row.sell_status==2" type="danger">{{row.sell_status_label}}</el-text>
          <el-text v-if="row.sell_status==3" type="info">{{row.sell_status_label}}</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="shangjia_status" label="上架状态" width="150">
        <template #default="{ row }">
          <el-radio-group v-model="row.shangjia_status" size="small" @change="shangjia_status_change(row,$event)">
            <el-radio-button :label="1">上架</el-radio-button>
            <el-radio-button :label="2">下架</el-radio-button>
          </el-radio-group>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDetail(row)">编辑</el-button>
          <el-button link type="primary" @click="handleCheck(row)">审核</el-button>
          <el-button link type="primary" @click="handleDong(row)">楼栋列表</el-button>
<!--          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消" @confirm="handleDelete(row)">-->
<!--            <template #reference>-->
<!--              <el-button link type="danger">删除</el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->

        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog v-model="checkDialogShow" title="审核" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeCheckDialog">
      <el-form ref="checkDialogFormRef" :model="checkDialogForm" :rules="checkDialogFormRules" label-width="120px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="checkDialogForm.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="check_cause">
          <el-input v-model.trim="checkDialogForm.check_cause" clearable placeholder="请输入审核意见" :rows="2"
                    type="textarea"/>
        </el-form-item>
        <el-form-item label="是否推荐" prop="is_tuijian">
          <el-radio-group v-model="checkDialogForm.is_tuijian">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">不是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="checkDialogParams.loading"
                       @click="submitCheckDialogForm(checkDialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(checkDialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  handleSearch,
  getData,
  handleAction,
  resetDialogForm
} = listMixins()

apiPath.value = 'admin/house/new/index?';
const router = useRouter()
const route = useRoute()
const searchParamsTime = ref('');
watch(searchParamsTime,(newV, oldV)=>{
  if(!newV){
    searchParams.s_time = ''
    searchParams.e_time = ''
  }else{
    searchParams.s_time = newV[0]
    searchParams.e_time = newV[1]
  }
})
onMounted(() => {
  getData()
})
const handleDelete = (row) => {
  handleAction('admin/house/new/delete', {
    new_house_id: row.new_house_id,
  })
}
const shangjia_status_change = (row, e) => {
  handleAction('admin/house/new/changeShangJiaStatus', {
    new_house_id: row.new_house_id,
    shangjia_status: e
  })
}

const handleDetail = (row) => {
  router.push({
    path: "/houseNew/houseNewDetail",
    query: {
      new_house_id: row.new_house_id
    }
  })
}
const handleDong = (row) => {
  router.push({
    path: "/houseNewDong/houseNewDong",
    query: {
      new_house_id: row.new_house_id,
      metaTitle:`楼栋列表【楼盘：${row.new_house_sn}-${row.title}-${row.developer_name}】`
    }
  })
}

const checkDialogShow = ref(false)
const checkDialogForm = reactive({
  new_house_id: '',
  status: 2,
  check_cause: '',
  is_tuijian:1
})
const checkDialogFormRef = ref(null)
const checkDialogParams = reactive({
  loading: false
})
const checkDialogFormRules = reactive({
  status: [{
    required: true,
    message: "请选择审核结果",
    trigger: "change"
  }],
  check_cause: [{
    required: true,
    message: "请输入审核意见",
    trigger: "blur"
  }]
})
const handleCheck = (row) => {
  checkDialogShow.value = true
  checkDialogForm.new_house_id = row.new_house_id
  checkDialogForm.status = 2
  checkDialogForm.check_cause = ''
  checkDialogForm.is_tuijian = 1
}
const closeCheckDialog = () => {
  checkDialogShow.value = false
  resetDialogForm(checkDialogFormRef.value)
}
const submitCheckDialogForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      checkDialogParams.loading = true
      let data = checkDialogForm;
      let url = 'admin/house/new/doCheck';
      proxy.$axios.post(url, data)
          .then((res) => {
            checkDialogParams.loading = false
            if (res.data.code == 200) {
              closeCheckDialog()
              proxy.$message({
                message: `成功`,
                type: "success"
              })
              getData()
            } else {
              proxy.$message({
                message: res.data.msg,
                type: "error"
              })
            }
          })
          .catch((err) => {
            checkDialogParams.loading = false
          })
    }
  })
}
</script>

<style lang="less">
.el-descriptions__cell {
  padding-bottom: 0 !important;
}

.el-descriptions__label {
  color: #999999 !important
}

</style>

